package app.xq.com.helloworldapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

import app.xq.com.helloworldapp.R;

/**
 * Created by Administrator on 2017/11/16.
 */

public class AccountsListAdapter extends BaseAdapter {
    private List<Map<String, Object>> data;
    private LayoutInflater layoutInflater;
    private Context context;

    /**
     * 构造函数,初始化ListView的各类方法
     *
     * @param context 全局上下文
     * @param data    初始的data对象
     */
    public AccountsListAdapter(Context context, List<Map<String, Object>> data) {
        this.context = context;
        this.data = data;
        this.layoutInflater = LayoutInflater.from(this.context);
    }

    /**
     * item 中的子元素集合类
     */
    public class ItemElement {
        public ImageView siteLogo;
        public TextView siteName;
        public TextView userAccounts;
        public TextView userPassword;
    }

    /**
     * 获取总数
     *
     * @return
     */
    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * 获取item
     *
     * @param i
     * @return
     */
    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    /**
     * 获取item 的id
     *
     * @param i
     * @return
     */
    @Override
    public long getItemId(int i) {
        return i;
    }

    /**
     * 获取Item中的视图
     *
     * @param i
     * @param view
     * @param viewGroup
     * @return
     */
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ItemElement item = new ItemElement();
        view = layoutInflater.inflate(R.layout.accounts_list_item, null);
        item.siteLogo = (ImageView) view.findViewById(R.id.siteLogo);
        item.siteName = (TextView) view.findViewById(R.id.siteName);
        item.userAccounts = (TextView) view.findViewById(R.id.userAccounts);
        item.userPassword = (TextView) view.findViewById(R.id.userPassword);
//        item.siteLogo.setImageURI((String)data.get(i).get("siteLogoUrl"));
        item.siteName.setText((String) data.get(i).get("siteUrl"));
        item.userAccounts.setText((String) data.get(i).get("accounts"));
        item.userPassword.setText((String) data.get(i).get("password"));
        return view;
    }
}
